package com.ynny.dao;

import com.ynny.bean.Student;
import com.ynny.bean.User;
import com.ynny.dao.StudentDao;
import com.ynny.dao.UserManage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class UserManageImpl implements UserManage {

    @Autowired
    private StudentDao studentDao;

    @Override
    public void addUser(User user) {
        // 假设 User 和 Student 类的属性相同，这里进行简单的转换
        Student student = new Student(user.getNumber(), user.getName(), user.getGrade(), user.getClbum(), user.getGender());
        studentDao.addStudent(student);
    }

    @Override
    public void quit() {
        // 实现退出逻辑
    }

    @Override
    public void updateUserByName(String name, User updatedUser) {
        Student student = new Student(updatedUser.getNumber(), updatedUser.getName(), updatedUser.getGrade(), updatedUser.getClbum(), updatedUser.getGender());
        studentDao.updateUserByName(name, student);
    }

    @Override
    public User getUserByNumber(String number) {
        Student student = studentDao.getStudentByNumber(number);
        if (student != null) {
            return new User(student.getNumber(), student.getName(), student.getGrade(), student.getClbum(), student.getGender());
        }
        return null;
    }

    @Override
    public List<User> getAllUsers() {
        List<User> users = new ArrayList<>();
        for (Student student : studentDao.getAllStudents()) {
            users.add(new User(student.getNumber(), student.getName(), student.getGrade(), student.getClbum(), student.getGender()));
        }
        return users;
    }
}
